Android

Notification

 

 

public void ShowNotification() {

    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

    // 채널을 생성하고 시스템에 등록 (오레오 API 26 이상부터는 채널 필요)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "My Channel 1";
        String description = "My Channel 1 Description";
        int importance = NotificationManager.IMPORTANCE_HIGH; // HIGH 옵션은 팝업 표시됨
        NotificationChannel channel = new NotificationChannel("12345", name, importance);  // 12345는 채널 ID
        channel.setDescription(description);
        notificationManager.createNotificationChannel(channel); // 채널을 시스템에 등록
    }

    // 알림 생성
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "12345")  // 12345는 채널 ID
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("My notification")
            .setContentText("Much longer text that cannot fit one line...")
            .setStyle(new NotificationCompat.BigTextStyle()
            .bigText("Much longer text that cannot fit one line... Much longer text that cannot fit one line...")) // 여러 줄 일때
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setAutoCancel(true) // 알림을 클릭하면 알림이 사라짐
            .setContentIntent(PendingIntent.getActivity(this, 0, new Intent(), 0));  // setAutoCancel 이 안먹힐때.

    notificationManager.notify(1234, builder.build()); // 1234는 알림 ID (알람을 삭제할 때 사용할 수 있음)
}

 

 


 

알림 만들기 – Android Developers
https://developer.android.com/training/notify-user/build-notification?hl=ko

 

Android Notification 예제 ( Oreo, androidx 적용 )
https://webnautes.tistory.com/665

 

[안드로이드/Android]8.0 오레오 알림채널(Notification Channel) 대응하기
https://gun0912.tistory.com/77

 

Notification setAutoCancel(true) doesn’t work
https://stackoverflow.com/questions/15033316/notification-setautocanceltrue-doesnt-work

 

 

Related posts

Leave a Comment